Skip to content

fix(sentry): isolate sentry runtime context per request - #1089

Open
huangdijia wants to merge 1 commit into
mainfrom
sentry-pr/r4
Open

fix(sentry): isolate sentry runtime context per request#1089
huangdijia wants to merge 1 commit into
mainfrom
sentry-pr/r4

Conversation

@huangdijia

@huangdijia huangdijia commented Aug 20, 2026

Copy link
Copy Markdown
Contributor

问题

HTTP/RPC 请求协程由引擎(Hyperf\Engine\Coroutine::create)创建,不走 Hyperf\Coroutine\Coroutine::create,Aspect\CoroutineAspect 不会为其 startContext,导致并发请求共享全局 Hub/Scope(面包屑/tag/user 跨请求串扰、scope 栈错位、请求级日志/指标无法按请求 flush)。

修改

  • Tracing listener 与基础 listener 的 handleRequestReceived 在请求事件中显式 SentrySdk::startContext() + defer(SentrySdk::endContext()),幂等、与 CoroutineAspect 兼容
  • Tracing listener 事务收尾 defer 改为使用捕获的请求 hub,不依赖 context 生命周期,避免污染共享全局 hub

测试

php /tmp/pest-runner-r4.php --group=sentry → 47 passed(80 assertions);CI/主 checkout 直接 vendor/bin/pest --group=sentry

验证

php-cs-fixer 0 修复;git diff --check 通过

@coderabbitai

coderabbitai Bot commented Aug 20, 2026

Copy link
Copy Markdown

Warning

Review limit reached

@huangdijia, you've reached your PR review limit, so we couldn't start this review.

Next review available in: 51 minutes

Limit details: You’ve used all 2 included reviews currently available.

You've used all free OSS reviews for now. Wait for the free limit to reset to keep reviewing this public repository.

How can I continue?

Wait for the limit to reset, then comment @coderabbitai review or push new commits to the PR.

An organization admin can change what happens after included review limits in Billing.

How do review limits work?

CodeRabbit enforces per-developer PR review limits within each organization.

For paid Pro and Pro+ reviews, CodeRabbit uses a developer's included PR review attempts over the past 7 days to set the current hourly allowance. At typical activity levels, the full plan allowance applies. Higher sustained activity can lower the allowance until earlier attempts leave the 7-day window.

Please refer docs for additional details.

Review details
⚙️ Run configuration

Configuration used: Path: .coderabbit.yaml

Review profile: CHILL

Plan: Pro Plus

Run ID: 5729d4d8-af64-48ed-bf1b-736d34018e75

📥 Commits

Reviewing files that changed from the base of the PR and between c814785 and 84d4712.

📒 Files selected for processing (3)
  • src/sentry/src/Listener/EventHandleListener.php
  • src/sentry/src/Tracing/Listener/EventHandleListener.php
  • tests/Sentry/RequestRuntimeContextTest.php

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands.

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 84d47127dd

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

// when the tracing listener already started one, and is ended via defer once the
// request coroutine exits.
SentrySdk::startContext();
defer(fn () => SentrySdk::endContext());

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 Badge Avoid scheduling a second context teardown

When both request handling and request tracing are enabled (the defaults), ConfigProvider invokes the tracing listener first and this base listener second. Although this listener's startContext() is a no-op for the already-active context, it still registers another LIFO endContext() defer; that defer runs before the tracing listener's transaction-finishing defer, removing and flushing the request context before the transaction is finished. Consequently, transaction telemetry is produced after the request's final flush and may be lost by transports that require flushing. Only the listener that creates the context should schedule its teardown, or context ownership should be centralized.

Useful? React with 👍 / 👎.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant